<?php

namespace app\api\service;

use think\facade\Db;

class SignInService
{
    /**
     * 获取当天用户签到记录
     * */

    public static function getCurrentDayRecord($user_id)
    {
        $sign =  Db::name('user_sign_in')->whereDay('sign_in_time')->where('user_id',$user_id)->find();

        return $sign;
    }

    /**
     * 获取当天用户签到记录
     * */

    public static function getYesterdayDayRecord($user_id)
    {
        $sign =  Db::name('user_sign_in')->whereDay('sign_in_time','yesterday')->where('user_id',$user_id)->find();

        return $sign;
    }


    /**
     * 获取店铺连续签到配置
     * */
    public static function getStoreSignInConfig($store_id)
    {
        $sign_in_config = Db::name('integral_setting')
                            ->field('sign_in_integral,sign_in_config')
                            ->where('store_id',$store_id)
                            ->withAttr('sign_in_config',function ($value){
                                if (empty($value)){
                                    return [];
                                }
                                return  json_decode($value,true);
                            })->find();

        return $sign_in_config;
    }

    /**
     * 获取优惠券信息
     * */
    public static function findCoupon($id)
    {
        $coupon = Db::name('coupon')
            ->field('coupon_name,coupon_type,icon,expire_type,expire_day,start_time,end_time,with_amount,used_amount')
            ->where('status',1)->where('id',$id)->find();

        return $coupon;
    }

    /**
     * 发起签到
     * */
    public static function saveSignIn($user_id,$total_num)
    {
        $result = Db::name('user_sign_in')
                    ->insert([
                        'user_id' => $user_id,
                        'sign_in_time' => time(),
                        'total_num' => $total_num
                    ]);

        return $result;
    }
}